package com.btpj.lib_base.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.text.TextUtils
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions

class GlideUtils {
    @SuppressLint("CheckResult")
    fun load(context: Context, url: String, imageView: ImageView, resource: Int) {
        val options = RequestOptions.circleCropTransform().apply {
            placeholder(resource).error(resource)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
        }
        Glide.with(context)
            .load(url)
            .apply(options)
            .into(imageView);
    }

    fun imgTailor(url: String, w: Int, h: Int): String? {
        return if (TextUtils.isEmpty(url)) {
            null
        } else {
            val a = url.split("[?]")
            a[0] + "?x-oss-process=image/resize,m_fill,w_" + w + ",h_" + h + ",limit_0/auto-orient,1/quality,q_90"
        }
    }

    fun loadImg(context: Activity, url: String, imageView: ImageView) {
        if (!context.isFinishing) {
            Glide.with(context)
                .load(url)
                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL))
                .into(imageView)
        }
    }

    fun loadImg(context: Context, url: String, imageView: ImageView) {
        Glide.with(context)
            .load(url)
            .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL))
            .into(imageView)
    }
}